﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextMining
{
    public class WordsSet   // Set containing dataset's words
    {
        // words are kept in a map
        private SortedList<string, Word> _words = new SortedList<string, Word>();

        public Word Add(string name)   // add word
        {
            name = name.ToLower();

            if (_words.ContainsKey(name) == true)  // don't add a word which is already in
                return _words[name];   // return word

            Word word = new Word(name);  // add new word
            _words.Add(name, word);

            return word;   // return word
        }

        public Word Get(string name)
        {
            name = name.ToLower();
            return _words[name];
        }

        public IEnumerable<Word> GetWords()   // get all words
        {
            return _words.Values;
        }

        // get a percentage of words sorted by the tf-idf method
        public IEnumerable<Word> TfIdfLessImportant(int documents, double percentage)
        {
            if (percentage < 0 || percentage > 100)
                return null;

            int count = (int)(percentage * _words.Count / 100);

            var query = from word in GetWords()
                        orderby word.TfIdfValue(documents) ascending
                        select word;
            return query.Take(count);
        }

        // get a percentage of words sorted by our method (tf)
        public IEnumerable<Word> OurMethodLessImportant(double percentage)
        {
            if (percentage < 0 || percentage > 100)
                return null;

            int count = (int)(percentage * _words.Count / 100);

            var query = from word in GetWords()
                        orderby word.OurMethodValue() ascending
                        select word;
            return query.Take(count);
        }
    }
}
